//
//  FFRecvPayRecvVM.m
//  LLWallet
//
//  Created by tt on 2020/12/24.
//

#import "FFRecvPayRecvVM.h"
#import "FFRecvPayRecvInputCell.h"
#import "FFRecvPayPayUserCell.h"
#import "FFRecvPayRecvSuccessVC.h"
#import "FFWalletManager.h"

@interface FFRecvPayRecvVM ()

@property(nonatomic,copy) NSString *amount;
@property(nonatomic,copy) NSString *remark;

@end

@implementation FFRecvPayRecvVM


- (NSArray<NSDictionary *> *)tableSectionMaps {
   WKChannelInfo*channelInfo = [[WKSDK shared].channelManager getChannelInfo:[[WKChannel alloc] initWith:self.uid channelType:WK_PERSON]];
    __weak typeof(self) weakSelf = self;
    return @[
        @{
            @"height": @(0.01f),
            @"items": @[
                    @{
                        @"class": FFRecvPayPayUserModel.class,
                        @"avatar": [WKAvatarUtil getAvatar:self.uid],
                        @"name": channelInfo.displayName?:@"",
                    }
            ],
        },
        @{
            @"height": @(10.0f),
            @"items": @[
                    @{
                        @"class": FFRecvPayRecvInputModel.class,
                        @"showBottomLine":@(YES),
                        @"onChange":^(NSString* value){
                            self.amount = value;
                        },
                        @"value": self.amount?:@"",
                        @"remark": weakSelf.remark?:@"",
                        @"onRemark":^(NSString *remark){ // 备注
                            weakSelf.remark = remark;
                            [weakSelf reloadData];
                        },
                        @"onDone":^{
                            if(!self.amount || self.amount.floatValue<=0) {
                                [[WKNavigationManager shared].topViewController.view showHUDWithHide:LLangW(@"收款金额不能等于或小于0！", weakSelf)];
                                return;
                            }
                            [self pay];
                        }
                    }
            ],
        },
    ];
}


-(void) pay {
    [[WKNavigationManager shared].topViewController.view showHUD];
    [[WKAPIClient sharedClient] POST:@"impay/recvpay/receive" parameters:@{
        @"vercode": self.vercode?:@"",
        @"amount": [WKMoneyUtil yuan2Penny:@(self.amount.floatValue)],
        @"remark": self.remark?:@"",
    }].then(^{
        [[WKNavigationManager shared].topViewController.view hideHud];
        FFRecvPayRecvSuccessVC *vc = [FFRecvPayRecvSuccessVC new];
        vc.uid = self.uid;
        vc.amount = [WKMoneyUtil yuan2Penny:@(self.amount.floatValue)].integerValue;
        [[WKNavigationManager shared] pushViewController:vc animated:YES];
        
    }).catch(^(NSError *error){
        [[WKNavigationManager shared].topViewController.view switchHUDError:error.domain];
    });
}



@end
